home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / ANSIshellƒ / os_mac_console.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-16  |  5.8 KB  |  274 lines  |  [TEXT/R*ch]

  1. /* os_mac_console.c
  2.  * 15Aug92  e
  3.  *
  4.  * The following functions are needed for console support...
  5.  *  __console_options (boot.c)
  6.  *  __cecho2file (ANSI Tricia π)
  7.  *  __cecho2printer (ANSI Tricia π)
  8.  *  __open_std (ANSI Tricia π)
  9.  *  __cshow (ANSI Tricia π)
  10.  * 
  11.  * and were shamelessly stolen from Symantec's console.c
  12.  * so that the eEdit code could be interfaced with the ANSI library
  13.  * so...
  14.  * portions Copyright © e 1992. All rights reserved.
  15.  * portions Copyright (c) 1991 Symantec Corporation.  All rights reserved.
  16.  */
  17.  
  18. #include <MacHeaders>
  19. #include <console.h>
  20. #include <stddef.h>
  21. #include <stdlib.h>
  22. #include <signal.h>
  23. #include <errno.h>
  24. #include <ansi_private.h>
  25.  
  26. #include "os_mac.h"
  27.  
  28. extern short interrupted;    /* from os_mac.c */
  29.  
  30. unsigned char *console_title; /* 14Jan95 for MW compat */
  31.  
  32. struct __copt console_options = { 50, 10, "\pconsole", 8, 4, 9, 0, 25, 80, 1 };
  33.  
  34. Boolean cshow_on_io;
  35.  
  36. /* closeecho - close echo file (if any) */
  37. /* unimplemented; was...
  38. static void closeecho(void)
  39. {
  40.     if (c.echo2fp)
  41.     {    if (c.spool)
  42.             print_console();
  43.         fclose(c.echo2fp);
  44.     }
  45. }
  46. */
  47.  
  48. /* cecho2file - echo console display to file */
  49.  
  50. void cecho2file(char *s, int append, FILE *fp)
  51. {
  52.     /* unimplemented; was...
  53.     struct save save;
  54.     setup(cflush(fp), &save);
  55.     closeecho();
  56.     c.echo2fp = fopen(s, append ? "a" : "w");
  57.     c.spool = 0;
  58.     restore(&save);
  59.     */
  60. }
  61.  
  62. /* cecho2printer - echo console display to printer */
  63.  
  64. void cecho2printer(FILE *fp)
  65. {
  66.     /* unimplemented; was...
  67.     struct save save;
  68.     setup(cflush(fp), &save);
  69.     closeecho();
  70.     c.echo2fp = tmpfile();
  71.     c.spool = 1;
  72.     restore(&save);
  73.     */
  74. }
  75.  
  76. /* cflush - flush all pending output to a console */
  77.  
  78. WindowPeek cflush(FILE *fp)
  79. {
  80.     WindowPeek wp = __checkfile(fp)->window;
  81.     int n;
  82.     for (fp = &__file[0], n = FOPEN_MAX; n--; fp++) {
  83.         if (fp->dirty && fp->window == wp)
  84.             fflush(fp);
  85.     }
  86.     return(wp);
  87. }
  88.  
  89. /* cshow - show a console window
  90.  *
  91.  *  All pending output to the window is forced to appear. */
  92.  
  93. void cshow(FILE *fp)
  94. {
  95.     WindowPeek wp = cflush(fp);
  96.     if (wp != (WindowPeek) FrontWindow())
  97.         SelectWindow( (WindowPtr )wp );
  98.     ShowWindow( (WindowPtr )wp );
  99. }
  100.  
  101. /*  __open_std - open the std streams
  102.  *
  103.  *  This is called automatically (by "__checkfile") whenever an
  104.  *  unopened std stream is referenced. */
  105.  
  106. void
  107. __open_std(void)
  108. {
  109.     FILE *fp = NULL;
  110.     char buf[40];
  111.     
  112.     _fcreator = CREATOR;
  113.     _ftype = 'BINA'; /* will be set to TEXT for text files */
  114.     if (stdin->std)
  115.         fp = freopenc(fp, stdin);
  116.     if (stdout->std)
  117.         fp = freopenc(fp, stdout);
  118.     if (stderr->std)
  119.         fp = freopenc(fp, stderr);
  120.     /* unimplemented; was...
  121.     if (__log_stdout)
  122.     {    sprintf(buf, "%#s.log", CurApName);
  123.         cecho2file(buf, 1, stdout);
  124.         console_options.pause_atexit = 0;
  125.     }
  126.     */
  127. }
  128.  
  129. static void maybe_cshow(FILE *fp)
  130. {    if (cshow_on_io) cshow(fp);
  131.     else             cflush(fp);
  132. }
  133.  
  134. extern long os_console_chars_ready(WindowPeek wp);
  135. long e_chars_ready(int fd)
  136. {
  137.     register FILE *fp;
  138.     
  139.     if ((unsigned) fd < FOPEN_MAX) {
  140.         fp = &__file[fd];
  141.         cflush(fp);
  142.         if (fp->window == (void *)(-1))
  143.             return fp->cnt;
  144.         else if (fp->window)
  145.             return( os_console_chars_ready(fp->window) );
  146.     }
  147.     return(0);
  148. }
  149.  
  150.  
  151. /* consoleio - I/O handler proc for console windows */
  152.  
  153. #if ReadHangsP
  154. static int kludgeflag; /* kludge 14Jan95 e */
  155. #endif
  156.  
  157. static int consoleio(FILE *fp, int i)
  158. {
  159.     int result = 0;
  160.     long  old_cnt = fp->cnt;
  161.     void *old_ptr = fp->ptr;
  162.     if (_abnormal_exit)
  163.         return(0);
  164.     switch (i)
  165.     {    case 0:                /*  read  */
  166. consoleio_read_again:
  167.             maybe_cshow(fp);
  168.             fp->eof = 0;
  169. #if ReadHangsP
  170.             if ( kludgeflag )
  171.             { /* kludge 14Jan95 e */
  172.               fp->cnt = 0;
  173.               fp->eof = 1;
  174.               result = EOF;
  175.               kludgeflag = 0;
  176.               break;
  177.             }
  178.             while ((fp->cnt = os_console_read_nohang(fp->window, fp->ptr, fp->cnt)) == 0)
  179.             {    fp->cnt = old_cnt;
  180.                 if (interrupted) break;
  181.             }
  182.             if ( fp->cnt != fp->size ) kludgeflag = 1; /* kludge 14Jan95 e */
  183. #else
  184.             if ((fp->cnt = os_console_read_nohang(fp->window, fp->ptr, fp->cnt)) == 0)
  185.             {    fp->eof = 1;
  186.                 result = EOF; /* why EOF !?? */
  187.             }
  188. #endif
  189.             break;
  190.         case 1:                /*  write  */
  191.             maybe_cshow(fp);
  192.             os_console_write(fp->window, fp->ptr, fp->cnt);
  193.             break;
  194.         case 2:                /*  close  */
  195.             os_console_close(fp->window);
  196.             break;
  197.     }
  198.     if (interrupted)
  199.     {    interrupted = 0;
  200.         FlushEvents(keyDownMask, 0);
  201.         fp->cnt = 0;
  202.         raise(SIGINT);
  203.         /* 15Sep92  e */
  204.         if( i == 0 )
  205.         { fp->cnt = old_cnt;
  206.           fp->ptr = old_ptr;
  207.           result = 0;
  208.           goto consoleio_read_again;
  209.         }
  210.         else
  211.         { errno = EINTR;
  212.           result = EOF; /* why EOF !?? */
  213.         }
  214.         /* */
  215.     }
  216.     return(result);
  217. }
  218.  
  219. /* console_exit - console shutdown routine */
  220.  
  221. extern Boolean gDoQuit;
  222.  
  223. static void console_exit(void)
  224. {    register FILE *fp;
  225.     int n;
  226.     /*  complete pending output  */
  227.     for (fp = &__file[0], n = FOPEN_MAX; n--; fp++)
  228.     {    if (fp->dirty && fp->window)
  229.             fflush(fp);
  230.     }
  231. #if pauseForQuitP
  232.     /*  pause for user acknowledgement  */
  233.     if ( !gDoQuit && console_options.pause_atexit )
  234.     {    for (fp = &__file[0], n = FOPEN_MAX; n--; fp++)
  235.         {    if (fp->window)
  236.             {    SetWTitle(fp->window, "\ppress «enter» to exit");
  237.                 setbuf(fp, NULL);
  238.                 while ( fgetc(fp) == EOF ) /* wait */ ;
  239.                 break;
  240.             }
  241.         }
  242.     }
  243. #endif
  244.     /*  close consoles  */
  245.     for (fp = &__file[0], n = FOPEN_MAX; n--; fp++)
  246.     {    if (fp->window)
  247.             fclose(fp);
  248.     }
  249. }
  250.  
  251. /*  freopenc - reopen a stream on a new or existing console
  252.  *
  253.  *  "fp" is closed, if necessary, then opened as a console.  If "fp2"
  254.  *  is NULL, a new console is created; otherwise "fp2" must refer to
  255.  *  a console, and "fp" is made to refer to the same console. */
  256.  
  257. FILE *freopenc(FILE *fp2, FILE *fp)
  258. {
  259.     if (fp == NULL)
  260.         return(NULL);
  261.     /* if (WWExist)  ???
  262.         InitConsole(); */
  263.     fclose(fp);
  264.     fp->refnum = -1;
  265.     if ( console_title != NULL ) console_options.title = console_title; /* 14Jan95 e */
  266.     fp->window = fp2 ? fp2->window : os_console_new( console_options.title );
  267.     setvbuf(fp, NULL, _IOLBF, BUFSIZ);
  268.     fp->proc = consoleio;
  269.     __atexit_console(console_exit);
  270.     return(fp);
  271. }
  272.  
  273. /* end of os_mac_console.c */
  274.